<?php

// 素材管理类

namespace Home\Controller;

// use Think\Controller;
class SucaiController extends BaseController {

    public function index() {
        $mod = D('sucai');
        $kmod = M("Keyword");
        $where = array('app_id' => $this->app['id'], 'state' => 0, "pid" => 0, "type" => "news");

        $data = $mod->where($where)->order('id desc')->select();
        foreach ($data as $k => &$v) {
            $tempData = $kmod->where("sid=" . $v['id'])->find();
            if ($tempData) {
                $v["keyword"] = $tempData["word"];
            }
        }

        $where_ext = array('app_id' => $this->app['id'], 'state' => 0, "pid" => 0, "type" => "newses");
        $pid_list = $mod->where($where_ext)->field("id")->order("id desc")->select();

        foreach ($pid_list as $pv) {

            $where_list = array('app_id' => $this->app['id'], 'state' => 0, "pid" => $pv["id"], "type" => "news");
            $data_list["news"] = $mod->where($where_list)->order("id asc")->select();
        }

        foreach ($data_list as &$dv) {
            $tempData = $kmod->where("sid=" . $dv[0]['pid'])->find();

            if ($tempData) {
                $dv["keyword"] = $tempData["word"];
            }
        }
        //var_dump($data_list);exit;
        $this->assign("datalist", $data_list);

        $this->assign('data', $data);


        $this->display();
    }

    public function double() {
        $this->display();
    }

    public function single() {
        $this->display();
    }

    public function text() {
        $smod = D("Sucai");
        $kmod = D("Keyword");

        $where = array("type" => "text", "app_id" => $this->app["id"], "state" => 0);
        $field = "id,content";
        $order = "id desc";
        $data = $smod->select_sucai($where, $field, $order);

        foreach ($data as &$v) {
            $temp = $kmod->where(array("type" => "text", "sid" => $v['id']))->getField("word");
            $v["kw"] = $temp;
            unset($temp);
        }

        $this->assign("data", $data);
        $this->display();
    }

    public function add_text() {
        $data = $_POST;

        $data["app_id"] = $this->app['id'];
        $data['state'] = 0;

        $smod = D("Sucai");
        $kmod = D("Keyword");
        $sdata = $smod->create($data);
        $data['sid'] = $smod->add_sucai($sdata);
        if ($data['sid']) {
            $kdata = $kmod->create($data);
            $kmod->add($kdata);
        }
    }

    public function image() {
        $this->display();
    }

    public function add_image() {
        $data = $_POST;

        $data["app_id"] = $this->app['id'];
        $data['state'] = 0;

        $smod = D("Sucai");
        $kmod = D("Keyword");


        $result = $this->upload_thing($data["cover"], $data["type"]);

        if (!isset($result["errmsg"])) {
            $sdata = $smod->create($data);
            $smod->add_sucai($sdata);

            $data["sid"] = $result["media_id"];
            $kdata = $kmod->create($data);
            $kmod->add($kdata);
        }
    }

    public function add_single() {
        $mod = D('sucai');
        if (($data = $mod->create($_POST))) {
            $data['app_id'] = $this->app['id'];
            if (($id = $mod->add($data)))
                $temp["url"] = "http://".$_SERVER['SERVER_NAME']."/News/index/id/".$id;
                $mod->where("id=".$id)->save($temp);
                $this->ajaxReturn(array('id' => $id));
        }
        $this->ajaxReturn(array('id' => '', 'msg' => '保存错误！'));
    }

    public function add_double() {
        $smod = D("Sucai");
        $title_arr = $_POST["title"];
        $content_arr = $_POST["content"];
        $pic_arr = $_POST["pic"];
        $url_arr = $_POST["url"];
        print_r($_POST);
        $len = count($title_arr);

        for ($i = 0; $i < $len; $i++) {
            $data[$i]["title"] = $title_arr[$i];
            $data[$i]["content"] = $content_arr[$i];
            $data[$i]["cover"] = $pic_arr[$i];
            $data[$i]["url"] = $url_arr[$i];
        }

        $temp_data = array("app_id" => $this->app["id"], "type" => "newses", "state" => 0);

        $pid = $smod->add_sucai($temp_data);

        $fill_data = array("pid" => $pid, "type" => "news", "state" => 0, "app_id" => $this->app["id"]);

        foreach ($data as &$v) {
            $temp = array_merge($fill_data, $v);
            $id =$smod->add_sucai($temp);
            $tempx["url"] = "http://".$_SERVER["SERVER_NAME"]."/News/index/id/".$id;
            $smod->where("id=".$id)->save($tempx);
            unset($temp);
        }
    }

}